import { onUnmounted, ref } from 'vue'
// 倒计时函数 hook

const useCountDown = function () {
  const formatTime = ref(0) // 格式化时间
  let timer: any = null
  const start = (currentTime: number) => {
    timer = setInterval(() => {
      if (currentTime > 0) {
        currentTime--
        formatTime.value = currentTime
      } else {
        // 0 的时候取消订单
        console.log(' 0 的时候取消订单,清除定时器')
      }
    }, 1000)
  }
  /* 组件销毁时停止倒计时*/
  onUnmounted(() => {
    timer && clearInterval(timer)
  })
  return {
    formatTime,
    start,
  }
}

export { useCountDown }

// 使用
//
// const { formatTime, start} = useCountDown()
// start(60)
